﻿using DotNetCore.CAP;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; 
using SHKF.B2C.BaseHosted; 

namespace SHKF.B2C.Hosted
{
    public static class HostedExtensions
    {
        public static IHostBuilder UseRun(this IHostBuilder hostBuilder, string hostJson)
        { 
            hostBuilder.UseRunCore(hostJson);
            //hostBuilder.ConfigureServices((context, services) =>
            //    {
            //        try
            //        {
            //            services.AddDBErp(context);
            //        }
            //        catch (System.Exception ex)
            //        {
            //            System.IO.File.AppendAllText("c://run.log", $"fun:UseRun>> {DateTime.Now} {ex.Message}\t\r");
            //        }
            //    });
            return hostBuilder;
        }
 
        /// <summary>
        /// 分仓数据库连接串
        /// </summary>
        /// <param name="services"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        //public static IServiceCollection AddDBErp(this IServiceCollection services, HostBuilderContext context)
        //{
        //    IConfigurationSection configurationSection = context.Configuration.GetSection("ServerList");
        //    services.Configure<List<DBServer>>(configurationSection);
        //    IOptions<List<DBServer>> dbList = services.BuildServiceProvider().GetService<IOptions<List<DBServer>>>();

        //    if (dbList != null)
        //    {
        //        WriteRunLog("//------------------------------添加DBServers:");

        //        //数据库实例列表
        //        foreach (DBServer dBServer in dbList.Value)
        //        {
        //            List<ConnectionConfig> iocList = new List<ConnectionConfig>();

        //            //数据库列表
        //            foreach (DBConfig dbEnum in dBServer.DBList)
        //            {
        //                string ip = dBServer.IP; //服务实例
        //                string dbConn = $"{ip}" + dbEnum.dbConn;

        //                ConnectionConfig connectionConfig = new ConnectionConfig();
        //                connectionConfig.ConfigId = dbEnum.dbName;
        //                connectionConfig.ConnectionString = dbConn;
        //                connectionConfig.DbType = DbType.SqlServer;
        //                connectionConfig.IsAutoCloseConnection = true;
        //                iocList.Add(connectionConfig);

        //                WriteRunLog($"dbName:{dbEnum.dbName} dbConn:{dbEnum.dbConn}");
        //            }

        //            SugarServiceExtensions.AddSqlSugar(dBServer.ServerName, iocList);
        //        }
        //    }

        //    return services;
        //}

        //private static void WriteRunLog(string context)
        //{
        //    string root = AppDomain.CurrentDomain.BaseDirectory;
        //    string runPath = root + "run";

        //    if (!System.IO.Directory.Exists(runPath))
        //    {
        //        Directory.CreateDirectory(runPath);
        //    }
        //    string path = $"{runPath}\\{DateTime.Now.ToString("yyyy-MM-dd")}.log";
        //    System.IO.File.AppendAllText(path, $"{DateTime.Now} {context}\r\n");

        //}
    }
}
